﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;

public partial class user : System.Web.UI.Page
{
    public int sign = 0;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminName"] == null)
            Response.Redirect("login.aspx");
        if (!Page.IsPostBack)
        {
            DAL.UserDALs userdals = new DAL.UserDALs();
            this.GridView1.DataSource = userdals.GetList();
            string[] keys ={ "User_ID" };
            this.GridView1.DataKeyNames = keys;
            this.GridView1.DataBind();
        }
    }

    protected void cmdAdd2_Click(object sender, EventArgs e)
    {
        DAL.UserDALs userdals = new DAL.UserDALs();

        User user = userdals.GetByName(this.txtName.Text);

        if (user == null)
        {
            userdals.Add(this.txtName.Text, this.txtPassword.Text, short.Parse(this.ddlArea.SelectedValue), this.txtEmail.Text,TxtQQ.Text,TxtMSN.Text,TxtWW.Text,TxtMobile.Text,TxtSMS.Text
                , memo.Text);
            this.GridView1.DataSource = userdals.GetList();
            this.GridView1.DataBind();
            Response.Redirect("user.aspx");
        }
        else
        {
            Response.Redirect("UserExist.aspx");
        }
        
    }

    protected string DGFormatSex(string xb)
    {
        if (xb == "1")
            return "在用";
        else
            return "停用";
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_Row(object sender, GridViewCommandEventArgs e)
    {

    }
 
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
         GridView1.PageIndex = e.NewPageIndex;
         DAL.UserDALs userdals = new DAL.UserDALs();
         this.GridView1.DataSource = userdals.GetList();
         this.GridView1.DataBind();
    }
    protected void cmdAddupdate_Click(object sender, EventArgs e)
    {
        DAL.UserDALs userdals = new DAL.UserDALs();
        User user = new User();
        user.User_id = int.Parse(this.user_id.Value);
        user.User_name = this.txtName.Text;
        user.Password = this.txtPassword.Text;
        user.Memo = this.memo.Text;
        user.Email = this.txtEmail.Text;
        user.Create_time = System.DateTime.Parse(this.create_time.Value);
        user.Status = short.Parse(this.ddlArea.SelectedValue);
        user.SMS = TxtSMS.Text;
        user.Qq = TxtQQ.Text;
        user.Ww = TxtWW.Text;
        user.Msn = TxtMSN.Text;
        user.Mobile = TxtMobile.Text;
        userdals.CreateUpdateDeleteUser(user, DAL.DataProviderAction.Update);
        Response.Redirect("user.aspx");
    }

    
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        
        //Response.Write(GridView1.DataKeys[e.NewEditIndex].Value);
        int userid = Convert.ToInt32(GridView1.DataKeys[e.NewEditIndex].Value);
        DAL.UserDALs userdals = new DAL.UserDALs();
        User user = userdals.GetByID(userid);
        this.txtName.Text = user.User_name;
        this.txtPassword.Text = user.Password;
        this.txtEmail.Text = user.Email;
        this.memo.Text = user.Memo;

        TxtSMS.Text = user.SMS;
        TxtQQ.Text = user.Qq;
        TxtWW.Text = user.Ww;
        TxtMSN.Text = user.Msn;
        TxtMobile.Text = user.Mobile;

        short no = user.Status;
       
        this.ddlArea.Items[this.ddlArea.SelectedIndex].Selected = false;
        if (no == 1)
            this.ddlArea.Items[0].Selected = true;
        else
            this.ddlArea.Items[1].Selected = true;

        this.user_id.Value = userid.ToString();
        this.create_time.Value = user.Create_time.ToLongDateString();

        sign = 1;

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        DAL.UserDALs userdals = new DAL.UserDALs();
        User user = userdals.GetByID(userid);
        if(user.User_name == "admin")
            Response.Redirect("admindonoetdelete.aspx");
        userdals.deleteUser(userid);
        this.GridView1.DataSource = userdals.GetList();
        string[] keys ={ "User_ID" };
        this.GridView1.DataKeyNames = keys;
        this.GridView1.DataBind();
        Response.Redirect("user.aspx");
    }
}
